package exp1;

import java.util.concurrent.*;

class Fibonacci_ extends RecursiveTask<Long> {
   final long n;
   Fibonacci_(long n) { this.n = n; }
   @Override
   public Long compute() {
     if (n <= 20) {
        return fibonacci(n);
     }
     ForkJoinTask<Long> subTask = new Fibonacci_( n - 1).fork();     
     return new Fibonacci_(n - 2).compute() + subTask.join();

   }
   static long fibonacci(long n) {
        if(n <= 1) {
            return n;
        }
        else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }
}

public class Fibonacci_Fork {
    public static void main(String[] args) {
    	for (int i = 1; i <= 48; i++){
        Fibonacci_ fibonacci = new Fibonacci_(i);
        ForkJoinPool mainPool = new ForkJoinPool();
        System.out.print(mainPool.invoke(fibonacci) + "\t\t");
 	   if ((i + 2) % 5 == 0) 
 		    System.out.println(); 
    	}
    }
}
